#include "myFunctions.h"
#include "bpNet.h"

int main(int argc,char *argv[]){
	bpNet Network;

	clock_t start;
	float **vRand;
	float **targets;
	vector<float> output;
	int error = 0;
	mdouble datosa, datosb,X;
	int numPatterns = 200, K = 10, maxEpocas = 1000;
	float deltaError = 0.015;
	vector<int> y(numPatterns),yd;

	int neurons[] = {2,2,1}; //Cargo topologia de la red
  
	genRandom("xor.txt", "xorRandomTest_pmc.txt",50, numPatterns);
	readFile(X,yd,"xorRandomTest_pmc.txt",true);

	vRand = (float**)calloc(numPatterns,sizeof(float *));
	targets = (float **)calloc(numPatterns,sizeof(float*));
	for (int i=0; i<numPatterns ; i++){
		vRand[i] = (float *)calloc(2,sizeof(float));
		targets[i] = (float *)calloc(1,sizeof(float));
	}     

	for (int i=0;i<numPatterns;i++){
		vRand[i][0] = X[i][1];
		vRand[i][1] = X[i][2];
		targets[i][0] = yd[i];
		//cout<<vRand[i][0]<<", "<<vRand[i][1]<<", "<<targets[i][0]<<endl;
	}

	Network.init(neurons,3); //Inicializo la red, training, test y default
	Network.RandomWeights(); //Pesos ramdom para iniciar el algoritmo de entrenamiento
	start = clock();
	Network.BpTrain(vRand,targets,numPatterns-K,maxEpocas,deltaError);
	cout<<"Tiempo de entrenamiento: "<<(clock()-start)/(double)CLOCKS_PER_SEC<<endl;
	output.resize(numPatterns);
	
	for (int i = 0; i < numPatterns; i++){
		output[i] = *(Network.getOutput(vRand[i]));  
    vector<double> aux;

		aux.push_back(vRand[i][0]);
		aux.push_back(vRand[i][1]);
		
		if(output[i]>0){
			datosa.push_back(aux);
		}
		else{
			datosb.push_back(aux);
		}
		if(sign(output[i]) != targets[i][0]) error++; 	}
	
	cout<<"Error de Red: "<<error<<" De: "<<numPatterns-K<<endl;
	saveFile(datosa,y,"xor_class_1_pmc.txt",false);
	saveFile(datosb,y,"xor_class_2_pmc.txt",false);
	
	Network.printData("xor_class_1_pmc.txt", "xor_class_2_pmc.txt");
	
	return 1;
}
